package cn.lili.modules.order.cart.render.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.modules.distribution.entity.dos.DistributionGoods;
import cn.lili.modules.distribution.entity.dos.DistributionRelation;
import cn.lili.modules.distribution.service.DistributionGoodsService;
import cn.lili.modules.distribution.service.DistributionRelationService;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.cart.entity.enums.RenderStepEnums;
import cn.lili.modules.order.cart.entity.vo.CartSkuVO;
import cn.lili.modules.order.cart.render.CartRenderStep;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.DistributionSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import com.xkcoding.http.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 分销佣金计算
 *
 * @author Chopper
 * @since 2020-07-02 14:47
 */
@Service
@Slf4j
public class DistributionPriceRender implements CartRenderStep {
    /**
     * 缓存
     */
    @Autowired
    private Cache cache;

    @Autowired
    private DistributionGoodsService distributionGoodsService;

    @Autowired
    private DistributionRelationService distributionRelationService;

    /**
     * 配置
     */
    @Autowired
    private SettingService settingService;

    @Override
    public RenderStepEnums step() {
        return RenderStepEnums.DISTRIBUTION;
    }

    @Override
    public void render(TradeDTO tradeDTO) {
        this.renderDistribution(tradeDTO);
    }

    /**
     * 渲染分销佣金
     *
     * @param tradeDTO
     */
    private void renderDistribution(TradeDTO tradeDTO) {

        //分销佣金修改
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        //如果存在分销员
//        String distributionId = (String) cache0.get(CachePrefix.DISTRIBUTION.getPrefix() + "_" + tradeDTO.getMemberId());
        DistributionRelation distributionRelation = distributionRelationService.lambdaQuery().eq(DistributionRelation::getDistributionSubId, currentUser.getId()).one();
        if (ObjectUtil.isEmpty(distributionRelation)) {
            return;
        }
        //循环订单商品列表，如果是分销商品则计算商品佣金
        tradeDTO.setDistributionId(distributionRelation.getDistributionId());

//        List<String> skuIds = tradeDTO.getCheckedSkuList().stream().map(cartSkuVO -> {
//            return cartSkuVO.getGoodsSku().getId();
//        }).collect(Collectors.toList());
        //是否包含分销商品
//        List<DistributionGoods> distributionGoods = distributionGoodsService.distributionGoods(skuIds);
//        if (distributionGoods != null && !distributionGoods.isEmpty()) {
//            distributionGoods.forEach(dg -> tradeDTO.getCheckedSkuList().forEach(cartSkuVO -> {
//                if (cartSkuVO.getGoodsSku().getId().equals(dg.getSkuId())) {
//                    cartSkuVO.setDistributionGoods(dg);
//                }
//            }));
//        }

//        for (CartSkuVO cartSkuVO : tradeDTO.getCheckedSkuList()) {
//            if (cartSkuVO.getDistributionGoods() != null) {
//                cartSkuVO.getPriceDetailDTO().setDistributionCommission(CurrencyUtil.mul(cartSkuVO.getNum(), cartSkuVO.getDistributionGoods().getCommission()));
//            }
//        }
        Setting setting = settingService.get(SettingEnum.DISTRIBUTION_SETTING.name());
        DistributionSetting distributionSetting = JSONUtil.toBean(setting.getSettingValue(), DistributionSetting.class);

        for (CartSkuVO cartSkuVO : tradeDTO.getCheckedSkuList()) {
//            if (cartSkuVO.getDistributionGoods() != null) {
            Double commission = CurrencyUtil.mul(cartSkuVO.getNum(), cartSkuVO.getUtilPrice());

            Double proportion = distributionSetting.getProportion();
            commission = CurrencyUtil.mul(commission, proportion);
            Integer distributionSettingCommission = distributionSetting.getCommission();
            log.info("商品佣金 = {}", commission);
            log.info("最小佣金 = {}", distributionSettingCommission);
            log.info("佣金比例 = {}", proportion);

            if (commission >= distributionSettingCommission) {
                log.info("赋值佣金 = {}",commission);
                cartSkuVO.getPriceDetailDTO().setDistributionCommission(commission);
            }

//            }
        }
    }
}
